// pages/vip/myvip/myvip.js
import {getTaocan,buyvip} from "../../../api/vipReq"
import updateInfo from "../../../api/getUserInfo"
import tologin from "../../../api/wxLogin"
Page({

  /**
   * 页面的初始数据
   */
  data: {
      showPop:true,
      taocan:[],
      current:"",
      mouth:"",
      price:0,
      userInfo:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

 async openPop(){
    if(getApp().globalData.userInfo){
      //已登录
      this.setData({
        showPop:false
      })
    }else{
      //未登录
      const res= await tologin();
      if(res.statusCode==200){
       this.setData({
         userInfo:getApp().globalData.userInfo
       });
      }
    }
  },
  closePop(){
      this.setData({
        showPop:true
      })
  },

  async getData(){
    const res=await getTaocan();
    if(res.meta.status==200){
      this.setData({
        taocan:res.data,
        current:res.data[0]._id,
        mouth:res.data[0].mouth,
      })
    }
  },
  chooseItem(event){
     const current=event.currentTarget.dataset.id;
     const mouth=event.currentTarget.dataset.mouth;
     const price=event.currentTarget.dataset.saleprice;
     this.setData({
      current,
      mouth,
      price
     }) 
  },
  async buyok(){
    wx.showLoading({
      title: '支付中'
    });
    const res=await buyvip({
      _id:getApp().globalData.userInfo._id,
      date:this.data.mouth
    });
    if(res.code){
      await  new Promise(resolve=>{
        setTimeout(function () {
          wx.hideLoading()
          resolve();
        }, 2500);  
      })

      const users=await updateInfo();
      this.setData({
        userInfo:users
      })
      wx.redirectTo({
          url: '/pages/vip/payok/payok?price='+this.data.price,
        });
        wx.showToast({
          title: res.msg,
          icon: 'success',
          duration: 2000
        });

    }else{
      await  new Promise(resolve=>{
        setTimeout(function () {
          wx.hideLoading()
          resolve();
        }, 2500);  
      })
      wx.showToast({
        title: res.msg,
        icon: 'error',
        duration: 2000
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      this.setData({
        userInfo:getApp().globalData.userInfo
      });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

 async loginvip(){
     if(getApp().globalData.userInfo!=null){
      this.setData({
        showPop:false
      })
     }else{
    
              //未登录
              const res= await tologin();
              if(res.statusCode==200){
               this.setData({
                 userInfo:getApp().globalData.userInfo
               });
              }
     }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})